/**
  ******************************************************************************
  * @file    APP_NORMAD.h
  * @author  Feirui_Joe
  * @version V0.1
  * @date    07/26/2010
  * @brief   Outputs Driver
  ******************************************************************************
  * @copy
  *
  * <h2><center>&copy; COPYRIGHT 2010 Feirui</center></h2>
  */ 



/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef  __APP_NORMAD_H__
#define  __APP_NORMAD_H__


#ifdef __cplusplus
 extern "C" {
#endif


/**
  * @brief  INCLUDE FILES.
  */


/**
  * @brief GLOBALS RAM definition
  */
#ifdef   APP_GLOBALS_NORMAD
#define  APP_EXT_NORMAD
#else
#define  APP_EXT_NORMAD  extern
#endif


/* StateUpdIn OS_Flags config bits */
#define OSNORMADFlags_Measure                      ((OS_FLAGS)DEF_BIT_00)
#define OSNORMADFlags_TempTiming                   ((OS_FLAGS)DEF_BIT_01)
#define OSNORMADFlags_TempRequest                  ((OS_FLAGS)DEF_BIT_02)
#define OSNORMADFlags_OilTiming                    ((OS_FLAGS)DEF_BIT_03)
#define OSNORMADFlags_OilRequest                   ((OS_FLAGS)DEF_BIT_04)
#define OSNORMADFlags_FuelTiming                   ((OS_FLAGS)DEF_BIT_05)
#define OSNORMADFlags_FuelRequest                  ((OS_FLAGS)DEF_BIT_06)
#define OSNORMADFlags_Sensor1Timing                ((OS_FLAGS)DEF_BIT_07)
#define OSNORMADFlags_Sensor1Request               ((OS_FLAGS)DEF_BIT_08)
#define OSNORMADFlags_Sensor2Timing                ((OS_FLAGS)DEF_BIT_09)
#define OSNORMADFlags_Sensor2Request               ((OS_FLAGS)DEF_BIT_10)
#define OSNORMADFlags_BatTiming                    ((OS_FLAGS)DEF_BIT_11)
#define OSNORMADFlags_BatRequest                   ((OS_FLAGS)DEF_BIT_12)
#define OSNORMADFlags_ChargeAltTiming              ((OS_FLAGS)DEF_BIT_13)
#define OSNORMADFlags_ChargeAltRequest             ((OS_FLAGS)DEF_BIT_14)
#define OSNORMADFlags_Reserve15                    ((OS_FLAGS)DEF_BIT_15)

#define OSNORMADFlags_TIMING                       (OSNORMADFlags_TempTiming    | \
                                                    OSNORMADFlags_OilTiming     | \
                                                    OSNORMADFlags_FuelTiming    | \
                                                    OSNORMADFlags_Sensor1Timing | \
                                                    OSNORMADFlags_Sensor2Timing | \
                                                    OSNORMADFlags_BatTiming     | \
                                                    OSNORMADFlags_ChargeAltTiming)

#define OSNORMADFlags_REQUEST                      (OSNORMADFlags_TempRequest    | \
                                                    OSNORMADFlags_OilRequest     | \
                                                    OSNORMADFlags_FuelRequest    | \
                                                    OSNORMADFlags_Sensor1Request | \
                                                    OSNORMADFlags_Sensor2Request | \
                                                    OSNORMADFlags_BatRequest     | \
                                                    OSNORMADFlags_ChargeAltRequest) 

#define OSNORMADFlags_ALL                          (OSNORMADFlags_TIMING|OSNORMADFlags_REQUEST|OSNORMADFlags_Measure)
#define OSNORMADFlags_DefaultValue                ((OS_FLAGS)    0x0000)




/******************************************/
/* OUT_PROTECT first- order filter Factor */
#define FTADCSENSOR1Factor                        ((double)0.20)       /* New*0.20*/
#define FTADCSENSOR2Factor                        ((double)0.20)       /* New*0.20*/
#define FTADCOILFactor                            ((double)0.20)       /* New*0.20*/
#define FTADCTEMPFactor                           ((double)0.20)       /* New*0.20*/
#define FTADCFUEL_LFactor                         ((double)0.20)       /* New*0.20*/
#define FTADCBAT_VFactor                          ((double)0.20)       /* New*0.20*/
#define FTADCCHARGEFactor                         ((double)0.20)       /* New*0.20*/
#define FTADCModuleTempFactor                     ((double)0.10)       /* New*0.10*/
                                                  
#define DefaultValHWCalibSENSOR1                  ((double)1.000)                 /* R Calib for 1 ohm  */
#define DefaultValHWCalibSENSOR2                  ((double)1.000)                 /* R Calib for 1 ohm  */
#define DefaultValHWCalibOIL                      ((double)1.000)                 /* R Calib for 1 ohm  */
#define DefaultValHWCalibTEMP                     ((double)1.000)                 /* R Calib for 1 ohm  */
#define DefaultValHWCalibFUEL_L                   ((double)1.000)                 /* R Calib for 1 ohm  */
#define DefaultValHWCalibBAT_V                    ((double)240/(double)2329)      /* Volt Calib for 0.1V */
#define DefaultValHWCalibCHARGE                   ((double)240/(double)2329)      /* Volt Calib for 0.1V */
#define DefaultValHWCalibModuleTemp               ((double)1.000)

/* ADC Input Last Value */
APP_EXT_NORMAD double ADCLastValue_BAT_V;
APP_EXT_NORMAD double ADCLastValue_SENSOR1;
APP_EXT_NORMAD double ADCLastValue_SENSOR2;
APP_EXT_NORMAD double ADCLastValue_OIL;
APP_EXT_NORMAD double ADCLastValue_TEMP;
APP_EXT_NORMAD double ADCLastValue_FUEL_L;
APP_EXT_NORMAD double ADCLastValue_CHARGE;
APP_EXT_NORMAD double ADCLastValue_ModuleTemp;
                                      
                                      
#define SensorOpenCircuitCNTLimit                  20                 /* 0.1*20=2S        */
APP_EXT_NORMAD uint8_t SensorOpenCircuitCntFUEL_L;
APP_EXT_NORMAD uint8_t SensorOpenCircuitCntOIL;
APP_EXT_NORMAD uint8_t SensorOpenCircuitCntTEMP;
APP_EXT_NORMAD uint8_t SensorOpenCircuitCntSENSOR1;
APP_EXT_NORMAD uint8_t SensorOpenCircuitCntSENSOR2;
APP_EXT_NORMAD uint8_t SensorOpenCircuitCntModuleTemp;

/* Fast Update Sensor Data */
#define SensorFastADCMeasureOpt

#define SensorFastUpdRangeLimit                     ((double)0.40)       /* Over/Under Range 0.40 */
#define SensorFastUpdCNTLimit                       10                   /* 0.1*10=1.0S     */
APP_EXT_NORMAD uint8_t SensorFastUpdCntFUEL_L;
APP_EXT_NORMAD uint8_t SensorFastUpdCntOIL;
APP_EXT_NORMAD uint8_t SensorFastUpdCntTEMP;
APP_EXT_NORMAD uint8_t SensorFastUpdCntSENSOR1;
APP_EXT_NORMAD uint8_t SensorFastUpdCntSENSOR2;
APP_EXT_NORMAD uint8_t SensorFastUpdCntModuleTemp;


/***********************/
/* Working Mode Status */

#define NormADCFilter_ModeInit                      ((uint8_t)0)
#define NormADCFilter_ModeWorking                   ((uint8_t)1)

APP_EXT_NORMAD uint8_t  ADCFilterMode_BAT_V  ;
APP_EXT_NORMAD uint8_t  ADCFilterMode_SENSOR1;
APP_EXT_NORMAD uint8_t  ADCFilterMode_OIL    ;
APP_EXT_NORMAD uint8_t  ADCFilterMode_SENSOR2;
APP_EXT_NORMAD uint8_t  ADCFilterMode_TEMP   ;
APP_EXT_NORMAD uint8_t  ADCFilterMode_FUEL_L ;
APP_EXT_NORMAD uint8_t  ADCFilterMode_CHARGE ;
APP_EXT_NORMAD uint8_t  ADCFilterMode_ModuleTemp;

/* Raw Data/Sta */
APP_EXT_NORMAD uint16_t  NormADRawData_BAT_V  ;
APP_EXT_NORMAD uint16_t  NormADRawData_SENSOR1;
APP_EXT_NORMAD uint16_t  NormADRawData_OIL    ;
APP_EXT_NORMAD uint16_t  NormADRawData_SENSOR2;
APP_EXT_NORMAD uint16_t  NormADRawData_TEMP   ;
APP_EXT_NORMAD uint16_t  NormADRawData_FUEL_L ;
APP_EXT_NORMAD uint16_t  NormADRawData_CHARGE ;
APP_EXT_NORMAD  int16_t  NormADRawData_ModuleTemp;

APP_EXT_NORMAD uint8_t   OpenCircuitSta_SENSOR1;
APP_EXT_NORMAD uint8_t   OpenCircuitSta_OIL    ;
APP_EXT_NORMAD uint8_t   OpenCircuitSta_SENSOR2;
APP_EXT_NORMAD uint8_t   OpenCircuitSta_TEMP   ;
APP_EXT_NORMAD uint8_t   OpenCircuitSta_FUEL_L ;
APP_EXT_NORMAD uint8_t   OpenCircuitSta_ModuleTemp;

/*********************************************/
/*********************************************/

/* Bat Alarm */
APP_EXT_NORMAD uint8_t BatAlarmSta_HighWarn;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t BatAlarmSta_LowWarn ;       /* 0:Inactive 1:Active */

APP_EXT_NORMAD uint16_t BatAlarmTiCnt_HighWarn;
APP_EXT_NORMAD uint16_t BatAlarmTiCnt_LowWarn ;

/* Charge Alarm */
APP_EXT_NORMAD uint8_t ChargeAlarmSta_LowWarn ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t DPlusAlarmSta_CrankOK  ;       /* 0:Inactive 1:Active */

APP_EXT_NORMAD uint16_t ChargeAlarmTiCnt_LowWarn ;
APP_EXT_NORMAD uint16_t DPlusAlarmTiCnt_CrankOK  ;

/*  Temp Alarm */
APP_EXT_NORMAD uint8_t TempAlarmSta_Heater    ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t TempAlarmSta_Cooler    ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t TempAlarmSta_HighShut  ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t TempAlarmSta_HighWarn  ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t TempAlarmSta_LowWarn   ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t TempAlarmSta_OpenCircuit;      /* 0:Inactive 1:Active */

APP_EXT_NORMAD uint16_t TempAlarmTiCnt_Heater    ;
APP_EXT_NORMAD uint16_t TempAlarmTiCnt_Cooler    ;
APP_EXT_NORMAD uint16_t TempAlarmTiCnt_HighShut  ;
APP_EXT_NORMAD uint16_t TempAlarmTiCnt_HighWarn  ;
APP_EXT_NORMAD uint16_t TempAlarmTiCnt_LowWarn   ;
APP_EXT_NORMAD uint16_t TempAlarmTiCnt_OpenCircuit;

APP_EXT_NORMAD uint32_t TempAlarmActCnt_Heater   ;
APP_EXT_NORMAD uint32_t TempAlarmActCnt_Cooler   ;

/* Oil Alarm */
APP_EXT_NORMAD uint8_t OilAlarmSta_LowWarn    ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t OilAlarmSta_LowShut    ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t OilAlarmSta_OpenCircuit;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t OilAlarmSta_GenRunning ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t OilAlarmSta_CrankOK    ;       /* 0:Inactive 1:Active */

APP_EXT_NORMAD uint16_t OilAlarmTiCnt_LowWarn    ;
APP_EXT_NORMAD uint16_t OilAlarmTiCnt_LowShut    ;
APP_EXT_NORMAD uint16_t OilAlarmTiCnt_OpenCircuit;
APP_EXT_NORMAD uint16_t OilAlarmTiCnt_GenRunning ;
APP_EXT_NORMAD uint16_t OilAlarmTiCnt_CrankOK    ;

/* Fuel Alarm */
APP_EXT_NORMAD uint8_t FuelAlarmSta_LowWarn    ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t FuelAlarmSta_OpenCircuit;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t FuelAlarmSta_PumpOn     ;       /* 0:Inactive 1:Active */

APP_EXT_NORMAD uint16_t FuelAlarmTiCnt_LowWarn    ;
APP_EXT_NORMAD uint16_t FuelAlarmTiCnt_OpenCircuit;
APP_EXT_NORMAD uint16_t FuelAlarmTiCnt_PumpOn     ;

APP_EXT_NORMAD uint32_t FuelAlarmActCnt_PumpOn    ;

/*  Sensor1 Alarm */
APP_EXT_NORMAD uint8_t Sensor1AlarmSta_OpenCircuit;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t Sensor1AlarmSta_HighShut   ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t Sensor1AlarmSta_HighWarn   ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t Sensor1AlarmSta_LowWarn    ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t Sensor1AlarmSta_LowShut    ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t Sensor1AlarmSta_GenRunning ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t Sensor1AlarmSta_CrankOK    ;       /* 0:Inactive 1:Active */

APP_EXT_NORMAD uint16_t Sensor1AlarmTiCnt_OpenCircuit;
APP_EXT_NORMAD uint16_t Sensor1AlarmTiCnt_HighShut   ;
APP_EXT_NORMAD uint16_t Sensor1AlarmTiCnt_HighWarn   ;
APP_EXT_NORMAD uint16_t Sensor1AlarmTiCnt_LowWarn    ;
APP_EXT_NORMAD uint16_t Sensor1AlarmTiCnt_LowShut    ;
APP_EXT_NORMAD uint16_t Sensor1AlarmTiCnt_GenRunning ;
APP_EXT_NORMAD uint16_t Sensor1AlarmTiCnt_CrankOK    ;

APP_EXT_NORMAD uint8_t  Sensor1AlarmSta_Heater   ; 
APP_EXT_NORMAD uint16_t Sensor1AlarmTiCnt_Heater ;
APP_EXT_NORMAD uint32_t Sensor1AlarmActCnt_Heater;

/*  Sensor2 Alarm */
APP_EXT_NORMAD uint8_t Sensor2AlarmSta_OpenCircuit;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t Sensor2AlarmSta_HighShut   ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t Sensor2AlarmSta_HighWarn   ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t Sensor2AlarmSta_LowWarn    ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t Sensor2AlarmSta_LowShut    ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t Sensor2AlarmSta_GenRunning ;       /* 0:Inactive 1:Active */
APP_EXT_NORMAD uint8_t Sensor2AlarmSta_CrankOK    ;       /* 0:Inactive 1:Active */

APP_EXT_NORMAD uint16_t Sensor2AlarmTiCnt_OpenCircuit;
APP_EXT_NORMAD uint16_t Sensor2AlarmTiCnt_HighShut   ;
APP_EXT_NORMAD uint16_t Sensor2AlarmTiCnt_HighWarn   ;
APP_EXT_NORMAD uint16_t Sensor2AlarmTiCnt_LowWarn    ;
APP_EXT_NORMAD uint16_t Sensor2AlarmTiCnt_LowShut    ;
APP_EXT_NORMAD uint16_t Sensor2AlarmTiCnt_GenRunning ;
APP_EXT_NORMAD uint16_t Sensor2AlarmTiCnt_CrankOK    ;

APP_EXT_NORMAD uint8_t  Sensor2AlarmSta_Heater   ; 
APP_EXT_NORMAD uint16_t Sensor2AlarmTiCnt_Heater ;
APP_EXT_NORMAD uint32_t Sensor2AlarmActCnt_Heater;

#define HeaterLevel_High        3
#define HeaterLevel_Return      2
#define HeaterLevel_Trip        1
#define HeaterLevel_Low         0

APP_EXT_NORMAD uint8_t  SensorActiveMode;

#define NormADInputTimingFreq                  10     /* 10HZ   */
#define SensorOpenCircuitTiDelay               5      /* 5s     */
#define OilGenRunningTripRate                  98     /* 98%    */
#define OilGenRunningTimLimit                  9      /* 0.9s   */
#define OilCrankTimLimit                       2      /* 0.2s   */

#define Sensor1GenRunningTimLimit              OilGenRunningTimLimit  
#define Sensor1CrankTimLimit                   OilCrankTimLimit       

#define Sensor2GenRunningTimLimit              OilGenRunningTimLimit  
#define Sensor2CrankTimLimit                   OilCrankTimLimit       

#define TempHeaterTiDelay                      2      /* 2s     */
#define TempCoolerTiDelay                      2      /* 2s     */
#define FuelPumpOnTiDelay                      2      /* 2s     */
#define Sensor1HeaterTiDelay                   2      /* 2s     */
#define Sensor2HeaterTiDelay                   2      /* 2s     */

#define DPlusCrankOKTimLimit                   2      /* 0.2s  */

#define OpenCircuitLimtRVal                    9600   /* 9600 ohm */

#define SensorOpenCircuitVal                   0xfffe
#define SensorNotUsedVal                       0xffff



APP_EXT_NORMAD void PORGlobalRAMReset_NORMAD(void);
APP_EXT_NORMAD void InitalOilNormADAlarmOperation(void);
APP_EXT_NORMAD void InitalSensor1NormADAlarmOperation(void);
APP_EXT_NORMAD void InitalSensor2NormADAlarmOperation(void);

APP_EXT_NORMAD uint8_t CheckIfSensorCurCurve(uint16_t SensorType,uint16_t CurveType);
APP_EXT_NORMAD double  CalADCAvgValue(uint16_t *StartAddr,uint8_t AddrInterval,uint8_t Number);

#ifdef __cplusplus
}
#endif


#endif   /*  __APP_NORMAD_H__  */

/******************* (C) COPYRIGHT 2010 Feirui *****END OF FILE****/